import 'package:flutter/material.dart';
import '../../core/enums/app_enums.dart';

/// 排序菜单组件 - Sort Menu Widget
/// 提供不同的排序选项
class SortMenu extends StatelessWidget {
  final SortType currentSortType;
  final Function(SortType) onSortChanged;

  const SortMenu({
    super.key,
    required this.currentSortType,
    required this.onSortChanged,
  });

  @override
  Widget build(BuildContext context) {
    return PopupMenuButton<SortType>(
      icon: const Icon(Icons.sort),
      tooltip: '排序',
      onSelected: onSortChanged,
      itemBuilder: (context) => [
        _buildMenuItem(
          context,
          SortType.updatedDesc,
          '最近更新',
          Icons.access_time,
          '按更新时间降序排列',
        ),
        _buildMenuItem(
          context,
          SortType.updatedAsc,
          '最早更新',
          Icons.history,
          '按更新时间升序排列',
        ),
        _buildMenuItem(
          context,
          SortType.createdDesc,
          '最近创建',
          Icons.fiber_new,
          '按创建时间降序排列',
        ),
        _buildMenuItem(
          context,
          SortType.createdAsc,
          '最早创建',
          Icons.schedule,
          '按创建时间升序排列',
        ),
        _buildMenuItem(
          context,
          SortType.titleAsc,
          '标题 A-Z',
          Icons.sort_by_alpha,
          '按标题字母顺序排列',
        ),
        _buildMenuItem(
          context,
          SortType.titleDesc,
          '标题 Z-A',
          Icons.sort_by_alpha,
          '按标题字母倒序排列',
        ),
        _buildMenuItem(
          context,
          SortType.sizeDesc,
          '文件大小',
          Icons.storage,
          '按文件大小降序排列',
        ),
      ],
    );
  }

  /// 构建菜单项 - Build Menu Item
  PopupMenuItem<SortType> _buildMenuItem(
    BuildContext context,
    SortType sortType,
    String title,
    IconData icon,
    String description,
  ) {
    final isSelected = currentSortType == sortType;

    return PopupMenuItem<SortType>(
      value: sortType,
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 4),
        child: Row(
          children: [
            Icon(
              icon,
              size: 20,
              color: isSelected ? Colors.blue : Colors.grey[600],
            ),
            const SizedBox(width: 12),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    title,
                    style: TextStyle(
                      fontWeight:
                          isSelected ? FontWeight.w600 : FontWeight.w500,
                      color: isSelected ? Colors.blue : null,
                    ),
                  ),
                  Text(
                    description,
                    style: Theme.of(context).textTheme.bodySmall?.copyWith(
                          color: Colors.grey[500],
                          fontSize: 11,
                        ),
                  ),
                ],
              ),
            ),
            if (isSelected)
              const Icon(
                Icons.check,
                size: 18,
                color: Colors.blue,
              ),
          ],
        ),
      ),
    );
  }
}

/// 排序类型扩展 - Sort Type Extension
extension SortTypeExtension on SortType {
  /// 获取排序类型的显示名称 - Get Display Name
  String getDisplayName() {
    switch (this) {
      case SortType.name:
        return '名称';
      case SortType.dateCreated:
        return '创建日期';
      case SortType.lastUsed:
        return '最后使用';
      case SortType.updatedDesc:
        return '最近更新';
      case SortType.updatedAsc:
        return '最早更新';
      case SortType.createdDesc:
        return '最近创建';
      case SortType.createdAsc:
        return '最早创建';
      case SortType.titleAsc:
        return '标题 A-Z';
      case SortType.titleDesc:
        return '标题 Z-A';
      case SortType.sizeDesc:
        return '文件大小';
    }
  }

  /// 获取排序类型的图标 - Get Icon
  IconData getIcon() {
    switch (this) {
      case SortType.name:
      case SortType.titleAsc:
      case SortType.titleDesc:
        return Icons.sort_by_alpha;
      case SortType.dateCreated:
      case SortType.createdDesc:
        return Icons.fiber_new;
      case SortType.createdAsc:
        return Icons.schedule;
      case SortType.lastUsed:
      case SortType.updatedDesc:
        return Icons.access_time;
      case SortType.updatedAsc:
        return Icons.history;
      case SortType.sizeDesc:
        return Icons.storage;
    }
  }

  /// 获取排序类型的描述 - Get Description
  String getDescription() {
    switch (this) {
      case SortType.name:
        return '按名称排序';
      case SortType.dateCreated:
        return '按创建日期排序';
      case SortType.lastUsed:
        return '按最后使用时间排序';
      case SortType.updatedDesc:
        return '按更新时间降序排列';
      case SortType.updatedAsc:
        return '按更新时间升序排列';
      case SortType.createdDesc:
        return '按创建时间降序排列';
      case SortType.createdAsc:
        return '按创建时间升序排列';
      case SortType.titleAsc:
        return '按标题字母顺序排列';
      case SortType.titleDesc:
        return '按标题字母倒序排列';
      case SortType.sizeDesc:
        return '按文件大小降序排列';
    }
  }
}
